package com.barryredmond.javamelibchecker;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Item;
import javax.microedition.lcdui.StringItem;


public class LibForm extends Form implements CommandListener{

    private final Command aboutCommand;
    private final Command exitCommand;
    
    private boolean cldc10;
    private boolean cldc11;
    private boolean midp1;
    private boolean midp2;
    private boolean jsr135;
    private boolean jsr120;
    private boolean jsr82;
    private boolean jsr75pim;
    private boolean jsr75file;
    private boolean jsr172;
    
    public LibForm() {
        super("Supported APIs");
        this.addCommand(aboutCommand = new Command("About", Command.ITEM, 1));
        this.addCommand(exitCommand = new Command("QUIT", Command.EXIT, 2));
        this.setCommandListener(this);
        
        checkAPISupport();
        
        StringItem title = new StringItem("Supported", null);
        this.append(title);

        // Append all supported APIs
        this.appendAll(true);
        
        
        
        title = new StringItem("NOT Supported", null);
        this.append(title);

        // Append all unsupported APIs.
        this.appendAll(false);
        
        // TODO Auto-generated constructor stub
    }

    private void checkAPISupport(){
        // Check that PIM Optional Package is available
        String temp;
        
        midp1 = true; // since we got this far in a "javax.microedition.lcdui.Form"
        
        try {
            Class.forName("javax.microedition.lcdui.game.GameCanvas");
            midp2 = true;
        }catch(Throwable e){
            midp2 = false;
        }
        
        // TODO: check line bellow.
        cldc10 = true; // I'm pretty sure this is a MINIMUM sys-requirement.... should check this.
        
        try {
            Class.forName("java.lang.ref.WeakReference");
            cldc11 = true;
        }catch(Throwable e){
            cldc11 = false;
        } 
        
        System.out.println("A");
        temp= System.getProperty( "microedition.pim.version" );
        if( temp != null ){
            // PIMOP available
            jsr75pim = true;
        } else {
            // PIMOP not available
            jsr75pim = false;
        }
        System.out.println("B");
        
        try {
            Class.forName("javax.microedition.io.file.FileSystemRegistry");
            jsr75file = true;
        }catch(Throwable e){
            jsr75file = false;
        }

        System.out.println("BC");
        try {
            Class.forName("javax.bluetooth.LocalDevice");
            jsr82 = true;
        }catch(Throwable e){
            jsr82 = false;
        }

        System.out.println("D");
        try {
            Class.forName("javax.wireless.messaging.TextMessage");
            jsr120 = true;
        }catch(Throwable e){
            jsr120 = false;
        }

        System.out.println("E");
        try {
            Class.forName("javax.microedition.media.Player");
            jsr135 = true;
        }catch(Throwable e){
            jsr135 = false;
        }

        System.out.println("F");
        try {
            Class.forName("javax.xml.parsers.SAXParser");
            jsr172 = true;
        }catch(Throwable e){
            jsr172 = false;
        }
        System.out.println("C");
        
    }

    private void appendAll(boolean supported){
        StringItem item;
        
        if( midp1 == supported){
            item = new StringItem(null, "MIDP 1.0");
            appendAPI(item);
        }
        
        if( midp2 == supported){
            item = new StringItem(null, "MIDP 2.0");
            appendAPI(item);
        }
        
        if( cldc10 == supported){
            item = new StringItem(null, "CLDC 1.0");
            appendAPI(item);
        }
        
        if( cldc11 == supported){
            item = new StringItem(null, "CLDC 1.1");
            appendAPI(item);
        }
        
        if( jsr75pim == supported){
            item = new StringItem(null, "jsr75pim");
            appendAPI(item);
        }
        
        if( jsr75file == supported){
            item = new StringItem(null, "jsr75file");
            appendAPI(item);
        }
        
        if(jsr82 == supported){
            item = new StringItem(null, "jsr82");
            appendAPI(item);
        }
        
        if( jsr120 == supported){
            item = new StringItem(null, "jsr120");
            appendAPI(item);
        }
        
        if(jsr135 == supported){
            item = new StringItem(null, "jsr135");
            appendAPI(item);
        }
        
        if(jsr172 == supported){
            item = new StringItem(null, "jsr172");
            appendAPI(item);
        }
        
    }
    
    private void appendAPI(Item item){
        append(item);
    }

    public void commandAction(Command command, Displayable disp) {
        if(disp == this){
            if(command == aboutCommand){
                Display.getDisplay(LibCheckerMIDlet.getMIDlet()).setCurrent(new AboutScreen(this));
            }else if(command == exitCommand){
                LibCheckerMIDlet.getMIDlet().notifyDestroyed();
            }
        }
        
    }

}
